﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using RestaurantBooker.Web.Data;
using System;
using RestaurantBooker.View;

namespace RestaurantBooker.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class OrderDetailsViewModel : ViewModelBase
    {
        #region Commands
        

        public RelayCommand<string> NewCustomerCommand { get; private set; }

        private void InitializeCommand()
        {
            NewCustomerCommand = new RelayCommand<string>((customerName) =>
                {
                    _customerDetailsViewModel.Customer = new Customer() { customer_name = customerName };
                    _newCustomerDlg.Value.Show();
                });
        }

        void _newOrderDlgViewModel_Closing(Customer customer)
        {
            CurrentOrder.Customer = customer;
        }
        #endregion


        #region NewCustomer
        private CustomerDetailsViewModel _customerDetailsViewModel = new CustomerDetailsViewModel();
        private Lazy<ModalDialogContainer> _newCustomerDlg = null;
        private Lazy<View.CustomerDetails> _lazyCustomerDetails = null;

         private void InitializeLazyMembers()
        {
             _lazyCustomerDetails = new Lazy<View.CustomerDetails>(() =>
            {
                return new View.CustomerDetails() { DataContext =  _customerDetailsViewModel};
            });

            _newCustomerDlg = new Lazy<ModalDialogContainer>(() =>
            {                
                var res = new ModalDialogContainer();
                res.Title = "New Customer";
                res.DialogContent = _lazyCustomerDetails.Value;
                res.OKButton.Content = "Create Customer";
                res.ConfirmationResultProc = (dlgRes) =>
                    {
                        if (dlgRes.HasValue && dlgRes.Value)
                        {
                            // add new customer
                            ServiceProcessor.ServiceProcessor.Instance.AddCustomer(_customerDetailsViewModel.Customer,
                                (error) =>
                                {
                                    if (error == null)
                                    {
                                        // customer succesfully added
                                        this.CurrentOrder.Customer = _customerDetailsViewModel.Customer;
                                    }
                                    else
                                    {
                                        this.CurrentOrder.Customer = null;
                                        throw error;
                                    }
                                });
                        }
                    };
                return res;
            });
        }

        #endregion

         #region Properties
         /// <summary>
        /// The <see cref="CurrentOrder" /> property's name.
        /// </summary>
        public const string CurrentOrderPropertyName = "CurrentOrder";

        private Order _currentOrder = null;

        /// <summary>
        /// Gets the CurrentOrder property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Order CurrentOrder
        {
            get
            {
                return _currentOrder;
            }

            set
            {
                if (_currentOrder == value)
                    return;

                _currentOrder = value;
                // Update bindings, no broadcast
                RaisePropertyChanged(CurrentOrderPropertyName);
            }
        }
        #endregion
        /// <summary>
        /// Initializes a new instance of the OrderDetailsViewModel class.
        /// </summary>
        public OrderDetailsViewModel()
        {
            InitializeCommand();
            InitializeLazyMembers();
        }
    }
}